package com.dq.service.finance.impl;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.RoyaltyRecordQuery;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.base.impl.BaseDaoImpl;
import com.dq.domain.org.Org;
import com.dq.domain.record.RoyaltyRecord;
import com.dq.repository.BaseRepositories;
import com.dq.service.finance.BossRoyaltyRecordService;
import com.dq.service.org.OrgService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author luchangtong
 * @Date 2019/12/23 10:28
 */
@Service
public class BossRoyaltyRecordServiceImpl extends BaseRepositories implements BossRoyaltyRecordService {
    @Autowired
    private OrgService orgService;
    @Override
    public Vo list(RoyaltyRecordQuery userOrderStaffQuery) throws AdminException {
        userOrderStaffQuery.format();
        Map<String, Object> query = userOrderStaffQuery.parseMap();
        if(userOrderStaffQuery.getOrgId() != null){
//            query.put("orders_org_id",userOrderStaffQuery.getOrgId());
            query.put("org_id",userOrderStaffQuery.getOrgId());
            query.remove("orgId");
        }else {
            // 只显示负责门店信息
            List<Org> orgList = orgService.getListAllByUser();
            if(orgList == null || orgList.size() <= 0) {
                throw new AdminException("此账号无负责门店");
            }
            query.put("org" + BaseDaoImpl.inSqlStr, orgList);
        }
        if(StringUtils.isNotEmpty(userOrderStaffQuery.getStaffUserNo())){
            query.put("sysUser_cardNo"+ BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(userOrderStaffQuery.getStaffUserNo()));
            query.remove("staffUserNo");
        }
        if(StringUtils.isNotEmpty(userOrderStaffQuery.getUserRealName())){
            query.put("sysUser_userRealcName"+ BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(userOrderStaffQuery.getUserRealName()));
            query.remove("userRealName");
        }
        if(userOrderStaffQuery.getBeginTime()!=null){
            query.put("createTime"+ BaseDaoImpl.gteSqlStr,userOrderStaffQuery.getBeginTime());
            query.remove("beginTime");
        }
        if(userOrderStaffQuery.getEndTime()!=null){
            query.put("createTime"+BaseDaoImpl.lteSqlStr,userOrderStaffQuery.getEndTime());
            query.remove("endTime");
        }
        List<RoyaltyRecord> list =  dao.find(dao.query2Hql(RoyaltyRecord.class,query),query);
        Long count = dao.count(dao.count2Hql(RoyaltyRecord.class,query),query);
        PageHelper.startPage(userOrderStaffQuery.getPage(),userOrderStaffQuery.getSize());
        PageInfo pageInfo = new PageInfo((List<RoyaltyRecord.Vo>) VO.toVOCollection(list));
        pageInfo.setTotal(count);
        return new Vo(pageInfo);
    }
}
